<template>
    <div class="container">
        <!-- 侧栏 -->
		<Sidebar/>
		<!-- 头部 -->
		<Header/>
        <div class="content Gwidth">
            <div class="left">
                <div class="left_nav">
                    <div 
                    class="item" id="leftNav" 
                     v-for="(item,index) in shop_list" :key="index"
                    :class="{active:activeIndex==index}" 
                    @click="navClick(index)">{{item.initials}}</div>
                    
                </div>
                <div class="left_cont" @scroll="_onScroll">
                    <div id="leftTitle" v-for="(item,index) in shop_list" :key="index">
                        <div class="title">{{item.initials}}</div>
                        <div class="cont">
                            <div class="item" v-for="shop in item.list" :key="shop.store_id"  @click="OpenCity(shop.store_id)">
                                <div class="logo">
                                    <img :src="shop.logo" :alt="shop.name" :title="shop.name">
                                </div>
                                <h1>{{shop.name}}</h1>
                            </div>
                            
                        </div>
                    </div>
                </div> 
            </div>
            <div class="right">
                <div class="right_title">条件筛选</div>
                <div class="selectall">
                    <div class="select_item">
                        <div class="title">类别：</div>
                        <div class="item_cont">
                            <p :class="{active:venue_active==-1}" @click="venueClick(-1)">全部</p>
                            <p v-for="(item,index) in venue" :key="index" :class="{active:venue_active==item.id}" @click="venueClick(item.id)">{{item.name}}</p>
                        </div>
                    </div>
                    <!-- <div class="select_item">
                        <div class="title">子类别：</div>
                        <div class="item_cont" :class="{active:switch_o_show}">
                            <p class="active">全部</p>
                            <p>办公家具</p>
                            <p>学校家具</p>
                            <p>医疗家具</p>
                            <p>商超家具</p>
                            <p>户外家具</p>
                            <p>公寓家具</p>
                            <p>娱乐家具</p>
                            <p>公寓家具</p>
                        </div>
                        <div class="switch" :class="{active:switch_o_show}" @click="switch_o_show=!switch_o_show">
                            <span>{{switch_o_show?'收起':'展开'}}</span>
                            <img src="~/static/image/ico/b_down.png" alt="">
                        </div>
                    </div> -->
                    <div class="select_item">
                        <div class="title">服务区域：</div>
                        <div class="item_cont" :class="{active:switch_t_show}">
                            <p :class="{active:region_active==-1}" @click="regionClick(-1)">全部</p>
                            <p v-for="(item,index) in region" :key='index' :class="{active:region_active==item.city_id}" @click="regionClick(item.city_id)">{{item.city_name}}</p>
                        </div>
                        <div class="switch" :class="{active:switch_t_show}" @click="switch_t_show=!switch_t_show">
                            <span>{{switch_t_show?'收起':'展开'}}</span>
                            <img src="~/static/image/ico/b_down.png" alt="">
                        </div>
                    </div>
                    <div class="select_item">
                        <div class="title">筛选条件：</div>
                        <div class="item_cont">
                            <div class="tag">
                                <span>{{venueName}}</span>
                                <img v-show="venue_active!=-1" @click="closeVenue" src="~/static/image/ico/close.png" alt="">
                            </div>
                            <div class="tag">
                                <span>{{areaName}}</span>
                                <img v-show="region_active!=-1" @click="closeArea" src="~/static/image/ico/close.png" alt="">
                            </div>
                            <!-- <div class="tag">
                                <span>石景山区</span>
                                <img src="~/static/image/ico/close.png" alt="">
                            </div> -->
                        </div>
                        <div class="del" @click="clearAll">清空条件</div>
                    </div>
                </div>
                <div class="shop_list">
                    <div class="top">
                        <div class="top_left">
                            <span>{{total}}</span>
                            <span>家厂家符合条件</span>
                        </div>
                        <div class="top_right">
                            <div class="Noselect item" :class="{active:enterprise_nav==1}" @click="ChangeNav(1)">综合推荐</div>
                            <div class="Noselect item" :class="{active:enterprise_nav==2}" @click="ChangeNav(2)">按热度排序</div>
                            <div class="Noselect item" :class="{active:enterprise_nav==3}" @click="ChangeNav(3)">按口碑排序</div>
                        </div>
                    </div>
                    <div class="btm">
                        <ShopList 
                        v-for="(item,index) in shoplist" 
                        :key="index" 
                        :logo="item.logo"
                        :citys="open_city"
                        :brand_name="item.brand_name"
                        :phone="item.phone"
                        :category_name="item.category_name"
                        :shopAddress="item.address"
                        :appoin_num="item.appoin_num"
                        :target_id="item.store_id"
                        />
                    </div>
                    <div class="loadingMore Noselect" @click="getIndexStoreList">
                        <Icon v-show="loading" type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
                        {{status}}
                    </div>
                </div>
            </div>
        </div>
        <!-- 上门尺量弹窗 -->
		<DoorToDoorRuler :citys="open_city" :target_id="target_id" :is_show="door_to_door_ruler" @onClose="onCloseRuler"/>
    </div>
</template>
<script>
import {mapState,mapMutations} from 'vuex'
import Header from "~/components/Header.vue";
import Sidebar from "~/components/Sidebar.vue";
import ShopList from "~/components/ShopList.vue";
import API from '@/http/api'
import http from '@/http/http.js'
export default {
    components: {
		Header,
		Sidebar,
        ShopList
	},
    data(){
        return{
            activeIndex:0,
            venue_active:-1,//场馆
            region_active:-1,//地区
            enterprise_nav:1,
            region:[],//地区列表
            open_city:[],//开通城市列表
            shop_list:[],//商家列表
            shoplist:[],//商家列表
            loadingMore:'loadmore',//加载更多
            target_id:'',
            door_to_door_ruler:false,
            total:0,
			page:2,
			limit:10,
            loading:false,
            switch_t_show:false,
            switch_o_show:false,
            is_scroll:true
        }
    },
    created(){
        this.getChildAreaList()
        this.shopStoreList()
        this.getIndexCategory()
        this.getOpenCityList()
        if (this.$route.query.id) {
            this.venue_active = this.$route.query.id
        }
        
    },
    computed:{
		...mapState(['address','venue']),
        status(){
			return this.loadingMore=="loading"?'加载中':this.loadingMore=="nomore"?'暂无数据':'加载更多'
		},
        venueName(){
            let name = '全部'
            if (this.venue_active == -1) {
                name = '全部'
            }
            this.venue.forEach(item => {
                if (item.id==this.venue_active) {
                    name = item.name
                }
            });
            return name
        },
        areaName(){
            let name = '全部'
            if (this.region_active == -1) {
                name = '全部'
            }
            this.region.forEach(item => {
                if (item.city_id==this.region_active) {
                    name = item.city_name
                }
            });
            return name
        }
	},
    async asyncData ({ store, params,route }) {
		let{data:{data:shoplist}} = await http.post(API.index.getIndexStoreList,{
			type:1,
			page:1,
			limit:10,
            receive_cate: route.query.id
		})
        return{
			shoplist:shoplist.data.data,
            total:shoplist.data.total
        }
    },
    mounted() {
		window.addEventListener('scroll', this.watchScroll,true)
	},
	destroyed(){
      window.removeEventListener('scroll', this.watchScroll,true);
    },
    methods:{
        watchScroll(e){
			// scrollTop 滚动条滚动时，距离顶部的距离
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			// windowHeight 可视区的高度
			var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
			// scrollHeight 滚动条的总高度
			var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
			// 滚动条到底部的条件
            if (Math.ceil(scrollTop + windowHeight+50) >= scrollHeight && this.is_scroll) {
				// 加载数据
                this.is_scroll = false
				this.getIndexStoreList()
			}
		},
        // 
        OpenCity(e){
            console.log('target_id-------------------------',e)
            this.target_id = e
            this.door_to_door_ruler = true
        },
        onCloseRuler(){
			this.door_to_door_ruler = false
		},
        // 选择地区
        regionClick(e){
            this.region_active = e
            this.page=1
			this.getIndexStoreList()
        },
        // 选择场馆
        venueClick(e){
            this.venue_active = e
            this.page=1
			this.getIndexStoreList()
        },
        // 关闭场馆分类
        closeVenue(){
            this.venue_active = -1
            this.page=1
			this.getIndexStoreList()
        },
        closeArea(){
            this.region_active = -1
            this.page=1
			this.getIndexStoreList()
        },
        clearAll(){
            this.venue_active = -1
            this.region_active = -1
            this.page=1
			this.getIndexStoreList()
        },
        // 开通城市列表
		async getOpenCityList(){
            let{data:{code,data}} = await this.$http.post(this.$API.getOpenCityList,{})
            if (code==200) {
                this.open_city = data.data
                
            }
        },
        // 场馆分类
		async getIndexCategory() {
			let {data:{code,data}} = await this.$http.post(this.$API.index.getIndexCategory,{})
			if(code==200){
				this.list = data.data
                this.$store.commit('ChangeVenue',data.data);
			}
		},
        // 商家排序
        async shopStoreList(){
            let {data:{code,data}} = await this.$http.post(this.$API.shopStoreList,{
                id:this.$route.query.id
            })
            if (code==200) {
                let tempArr = [], newArr = []
                for (let i = 0; i < data.data.length; i++) {
                    if (tempArr.indexOf(data.data[i].initials) === -1) {
                        newArr.push({
                            initials: data.data[i].initials,
                            list: [data.data[i].data]
                        })
                        tempArr.push(data.data[i].initials);
                    } else {
                        for (let j = 0; j < newArr.length; j++) {
                            if (newArr[j].initials == data.data[i].initials) {
                                newArr[j].list.push(data.data[i].data)
                            }
                        }
                    }
                }
                for (let i = 0; i < newArr.length; i++) {
                    for (let y = 0; y < newArr.length; y++) {
                        if (newArr[i].initials<newArr[y].initials) {
                            let item = newArr[y]
                            newArr[y] = newArr[i]
                            newArr[i] = item
                        } 
                        
                    }
                }
                this.shop_list = newArr
                
            }
        },
        // 二级城市列表
        async getChildAreaList(){
            let {data:{code,data}} = await this.$http.post(this.$API.getChildAreaList,{
                province:this.address.province
            })
            if (code==200) {
                this.region = data.data
            }
        },
        // 切换分类
		ChangeNav(e){
			this.enterprise_nav = e
			this.page=1
			this.getIndexStoreList()
		},
        // 商家列表
		async getIndexStoreList(){
			this.loading = true
			this.loadingMore = 'loading'
			if (this.page==1) {
				this.shoplist = []
			}
			let{data:{code,data,msg}} = await this.$http.post(this.$API.index.getIndexStoreList,{
				type:this.enterprise_nav,
				receive_cate:this.venue_active==-1?'':this.venue_active,
                city:this.region_active==-1?'':this.region_active,
				page:this.page,
				limit:this.limit,
			})
			this.loading = false
			if (code==200) {
                this.total=data.data.total
				if (data.data.data.length>=this.limit) {
					this.loadingMore = 'loadmore'
				}else{
					this.loadingMore = 'nomore'
				}
				if (data.data.data.length>0) {
					this.shoplist = this.shoplist.concat(data.data.data)
					this.page += 1
				}
			}else{
				this.loadingMore = 'nomore'
			}
			this.is_scroll = true
		},
        navClick(e){
            this.activeIndex = e
            var items = document.querySelectorAll("#leftTitle");
            for (var i = 0; i < items.length; i++) {
                if (e === i) {
                    items[i].parentNode.scrollTop = items[i].offsetTop;
                    // items[i].scrollIntoView({
                    //     block: "start",//默认跳转到顶部
                    //     behavior: "smooth"//滚动的速度
                    // });
                }
            }
        },
        _onScroll(e) {
            let scrollItem = document.querySelectorAll("#leftTitle");
            for (let i = scrollItem.length - 1; i >= 0; i--) {
                // 判断滚动条滚动距离是否大于当前滚动项可滚动距离
                let scroll_top = e.target.scrollTop >= scrollItem[i].offsetTop - scrollItem[0].offsetTop;
                if (scroll_top) {
                    this.activeIndex = i;
                    break;
                }
            }
        },
    },
}
</script>
<style scoped>
    .container .content{
        margin-top: 30px;
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
    }
    .container .content .left{
        width: 300px;
        display: flex;
        border: 1px solid #E6E6E6;
        position:sticky;
        left: 0;
        top:72px
    }
    .container .content .left .left_nav{
        border-right: 1px solid #E6E6E6;
        text-align: center;

    }
    .container .content .left .left_nav .item{
        width: 27px;
        height: 27px;
        font-size: 16px;
        font-weight: 400;
        line-height: 27px;
        cursor: pointer;
        margin-bottom: 9px;
    }
    .container .content .left .left_nav .item:last-child{
        margin-bottom: 0;
    }
    .container .content .left .left_nav .item.active{
        background: #F30213;
        color: #FFFFFF;
    }
    .container .content .left .left_cont{
        width: calc(100% - 27px);
        height: 800px;
        overflow-y: auto;
    }
    .container .content .left .left_cont .title{
        height: 27px;
        background: #F8F8F8;
        font-size: 12px;
        font-weight: 400;
        color: #333333;
        line-height: 27px;
        padding-left: 17px;
    }
    .container .content .left .left_cont .cont .item{
        display: flex;
        align-items: center;
        padding:0 14px;
        margin-top: 24px;
    }
    .container .content .left .left_cont .cont .item .logo{
        width: 32px;
        height: 32px;
        border-radius: 4px;
        border: 1px solid #cccccc;
        line-height: 32px;
        overflow: hidden;
        font-size: 0;
    }
    .container .content .left .left_cont .cont .item .logo img{
        max-width: 32px;
        max-height: 32px;
        border-radius: 4px;
        vertical-align: middle;
    }
    .container .content .left .left_cont .cont .item h1{
        font-size: 16px;
        font-weight: 400;
        color: #333333;
        margin-left: 14px;
    }
    .container .content .left .left_cont .cont .item:last-child{
        margin-bottom: 24px;
    }
    .container .content .right{
        width: 863px;
    }
    .container .content .right .selectall .select_item,.container .content .right .selectall .select_item .item_cont{
        display: flex;
    }
    .container .content .right .right_title{
        font-size: 20px;
        font-weight: 400;
        color: #333333;
    }
    .container .content .right .selectall{
        margin-top: 25px;
    }
    .container .content .right .selectall .select_item{
        padding: 15px 0;
        border-top: 1px solid #eeeeee;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        position: relative;
    }
    .container .content .right .selectall .select_item .title{
        width: 70px;
        color: #666666;
        padding-top: 7px;
    }
    .container .content .right .selectall .select_item .item_cont{
        flex-wrap: wrap;
        width: calc(100% - 200px);
        height: 35px;
        overflow: hidden;
    }
    .container .content .right .selectall .select_item .item_cont.active{
        height: auto;
    }
    .container .content .right .selectall .select_item .item_cont p{
        padding: 7px 10px;
        border-radius: 4px;
        margin: 0 4px;
        cursor: pointer;
    }
    .container .content .right .selectall .select_item .item_cont p.active{
        color: #ffffff;
        background-color: #F30213;
    }
    .container .content .right .selectall .select_item .switch{
        position: absolute;
        right: 0;
        top: 15px;
        line-height: 35px;
        cursor: pointer;
    }
    .container .content .right .selectall .select_item .switch img{
        width: 14px;
        height: 8px;
        transition: 0.35s;
    }
    .container .content .right .selectall .select_item .switch.active img{
        transform:rotate(180deg);
    }
    .container .content .right .selectall .select_item .item_cont .tag{
        border: 1px solid #F30213;
        font-size: 0;
        padding: 0 10px;
        border-radius: 4px;
        margin-left: 14px;
        line-height: 33px;
        display: flex;
        align-items: center;
    }
    .container .content .right .selectall .select_item .item_cont .tag span{
        font-size: 14px;
        line-height: 14px;
    }
    .container .content .right .selectall .select_item .item_cont .tag img{
        width: 11px;
        height: 11px;
        margin-left: 10px;
        cursor: pointer;
    }
    .container .content .right .selectall .select_item .del{
        position: absolute;
        right: 0;
        top: 24px;
        background: url(~/static/image/ico/del.png)left center no-repeat;
        background-size: 12px 16px;
        padding-left: 20px;
        cursor: pointer;
    }
    .container .content .right .shop_list .top{
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-top: 26px;
        border-top: 1px solid #eeeeee;
    }
    .container .content .right .shop_list .top .top_right{
        display: flex;
        align-items: center;
    }
    .container .content .right .shop_list .top .top_right .item{
        margin-left: 40px;
        font-size: 14px;
        font-weight: 400;
        color: #666666;
    }
    .container .content .right .shop_list .top .top_right .item.active{
        font-weight: bold;
    }
    .container .content .right .shop_list .top .top_left{
        font-size: 14px;
        font-weight: 400;
    }
    .container .content .right .shop_list .top .top_left span:first-child{
        color: #F30213;
    }
    .shop_list .btm #shop_list:last-child{
        border-bottom: 0;
    }
</style>
